package com.ntanh.mealplanner.main.objects;

import android.content.ContentValues;
import android.util.Log;

import com.ntanh.mealplanner.base.model.BaseEntity;
import com.ntanh.mealplanner.base.model.NamedEntity;
import com.ntanh.mealplanner.main.dao.DataBaseHelper;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Anh Nguyen on 2/25/2015.
 */
public class WeekMeal extends NamedEntity {

    public static final String DAY_MEAL_LIST = "dayMealList";

    public static final String START_DATE = "startDate";

    private List<DayMeal> dayMealList = new ArrayList<DayMeal>();

    private Date startDate;

    public WeekMeal(String author, List<DayMeal> dayMealList, Date startDate) {
        super(author);
        this.dayMealList = dayMealList;
        this.startDate = startDate;
    }

    public WeekMeal(String author, Date startDate) {
        super(author);
        this.startDate = startDate;
        this.dayMealList = new ArrayList<DayMeal>();
    }

    public WeekMeal(String author, String name, Date startDate) {
        super(author);
        this.name = name;
        this.startDate = startDate;
        this.dayMealList = new ArrayList<DayMeal>();
    }

    public WeekMeal(String author) {
        super(author);
        this.dayMealList = new ArrayList<DayMeal>();

        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_WEEK, 2);
        this.startDate = calendar.getTime();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof WeekMeal)) return false;
        if (!super.equals(o)) return false;

        WeekMeal weekMeal = (WeekMeal) o;

        if (dayMealList != null && weekMeal.dayMealList != null && dayMealList.size() == weekMeal.dayMealList.size()) {
            for (int i = 0; i < dayMealList.size(); i++) {
                if (!dayMealList.get(i).equals(weekMeal.dayMealList.get(i))) {
                    return false;
                }
            }
        } else {
            return false;
        }

        if (startDate != null ? !startDate.equals(weekMeal.startDate) : weekMeal.startDate != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        if (dayMealList != null) {
            int hash = 0;
            for (DayMeal dayMeal : dayMealList) {
                hash += dayMeal.hashCode();
            }
            result = 31 * result + hash;
        }
        result = 31 * result + (startDate != null ? startDate.hashCode() : 0);
        return result;
    }

    public List<DayMeal> getDayMealList() {
        return dayMealList;
    }

    public void setDayMealList(List<DayMeal> dayMealList) {
        this.dayMealList = dayMealList;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public JSONObject toJSONObject() {

        JSONObject json = super.toJSONObject();
        try {
            if (json != null) {
                json = new JSONObject();
            }

            if (dayMealList != null) {
                JSONArray array = new JSONArray();

                for (DayMeal dayMeal : dayMealList) {
                    array.put(dayMeal.toJSONObject());
                }

                json.put(WeekMeal.DAY_MEAL_LIST, array);
            }

            json.put(WeekMeal.START_DATE, startDate);

        } catch (Exception e) {
            Log.d("Convert error", "Cannot convert WeekMeal to JSON", e);
        }

        return json;
    }

    public void toContentValue(ContentValues values, boolean forInserting) {
        super.toContentValue(values, forInserting);
        values.put(START_DATE, BaseEntity.dateFormatter.format(getStartDate()));
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < dayMealList.size() - 1; i++) {
            sb.append(dayMealList.get(i).getId());
            sb.append(BaseEntity.entitySplitSign);
        }
        if (dayMealList.size() > 0) {
            sb.append(dayMealList.get(dayMealList.size() - 1).getId());
        }
        values.put(DAY_MEAL_LIST, sb.toString());
    }

    @Override
    public String getTableName() {
        return DataBaseHelper.WEEK_MEAL_TABLE;
    }
}
